
/*
需求: 当有多个线程,执行不同的操作,针对于同一个资源时,有先存后取的顺序,需要交替执行
此时需要等待唤醒机制保证线程的交替执行


*/

class Res {

	boolean flag = false;
	private String name;
	private String sex;

	public synchronized void setInfo(String name,String sex){
		if (flag) 
			try{this.wait();}catch(Exception e){};
		this.name = name;
		this.sex = sex;
		flag = true;
		this.notify();
		
	}

	public synchronized void outInfo(){
		
		if (!flag) 
			try{this.wait();}catch(Exception e){};
		System.out.println(this.name+" ----- "+this.sex);
		flag = false;
		this.notify();
		
	}
}

class Input implements Runnable {
	private Res r;
	
	public Input (Res r){
		this.r = r;
	}
	public void run(){
		int x = 0;
		while(true){
			if (x == 0) {
				this.r.setInfo("腐化","男");
			}else{
				this.r.setInfo("堕落","女");
			}
			x = (x+1)%2;
		}
	}
}

class Output implements Runnable {
	private Res r;

	public Output (Res r){
		this.r = r;
	}
	public void run(){
		while(true){
			this.r.outInfo();
		}
		
	}
}


class WaitNoti {

	public static void main(String[] args) {
		Res r = new Res();
		new Thread(new Input(r)).start();
		new Thread(new Output(r)).start();
	}

}























